//
//  PopoverPresentationController.swift
//  SinaBlog
//
//  Created by ChenWei on 15/10/5.
//  Copyright © 2015年 cw. All rights reserved.
//



import UIKit

/// 菜单弹出和消失的通知
let CWNotificationPopoverViewDidShow = "CWNotificationPopoverViewDidShow"
let CWNotificationPopoverViewDidDismiss = "CWNotificationPopoverViewDidDismiss"

class PopoverPresentationController: UIPresentationController, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
    
    /// 记录转场状态
    var isPresented : Bool = false
    
    /// 转场界面的frame
    var presentedViewFrame: CGRect = CGRectZero
    
    // MARK: - UIViewControllerTransitioningDelegate : 返回转场容器以及实现转场界面动态添加或者移出转场容器的控制器.
    // 返回一个转场界面容器控制器(用来添加转场界面)
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        let pc = CWPresentationController(presentedViewController: presented, presentingViewController: presenting)
        pc.presentedViewFrame = presentedViewFrame
        return pc
    }
    
    // 返回一个添加转场界面到转场容器的控制器: 转场界面展现时调用
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        NSLog(message: "展现")
        
        NSNotificationCenter.defaultCenter().postNotificationName(CWNotificationPopoverViewDidShow, object: nil)
       
        isPresented = true
       
        return self
    }
    
    // 返回一个从转场容器移除转场界面的控制器:转场界面移出时调用
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        NSLog(message: "消失")
        
        NSNotificationCenter.defaultCenter().postNotificationName(CWNotificationPopoverViewDidDismiss, object: nil)
        
        isPresented = false
        
        return self
    }

    // MARK: - UIViewControllerAnimatedTransitioning : 实现转场界面动态添加或者移出转场方法:由负责控制转场动画的控制器调用.
    // 实现转场动画的展现和消失
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        // 1. 转场动画
        if isPresented {  // 2.1 展现
            // 1.添加转场界面
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            transitionContext.containerView()?.addSubview(toView)

            toView.transform = CGAffineTransformMakeScale(1, 0.0)
            NSLog(message: transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey))
            NSLog(message: transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey))
            // 2.动态显示
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                toView.transform = CGAffineTransformMakeScale(1, 1)
                
               toView.layer.anchorPoint = CGPointMake(0.5, 0)
               
                }, completion: { (_) -> Void in
                    transitionContext.completeTransition(true)
            })
        } else { // 2.2. 消失
            let view = transitionContext.viewForKey(UITransitionContextFromViewKey)
            // 1.动态隐藏转场界面
            UIView.animateWithDuration(0.5, animations: { () -> Void in
                view?.transform = CGAffineTransformMakeScale(1, 0.000001)
                }, completion: { (_) -> Void in
                    // 2.告诉系统转场结束
                    transitionContext.completeTransition(true)
            })
        }
    }
    
    /// 转场的动画时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    
    deinit{
        NSLog(message: "UIPresentationControler")
    }
    
}






